#!/usr/local/bin/bash


#保存当前用户环境变量路径
env_var_path=~/.bash_profile

# 设置财务分配工具环境变量
script_abs=`pwd`
alloc_dir="$script_abs/platon-alloc_for_MacOS/"
echo $alloc_dir


# 旧版本工具路径
old_alloc_dir=""
old_path=""
while read line
do
	k=${line%=*}
    v=${line#*=}
	if [[ $k == "export ALLOCPATH" ]];then
		old_alloc_dir=$v
	fi

    if [[ $k == "export PATH" ]];then
        echo $v | grep 'ALLOCPATH'
        # 找到ALLOCPATH
        if [ 0 -eq $? ];then
            old_path=$v
        fi
    fi
done < $env_var_path

echo "old alloc dir:$old_alloc_dir"
echo "alloc_dir: $alloc_dir"
echo "old path: $old_path"

if [ -z $old_alloc_dir ];then 
	# 未设置环境变量，添加环境变量
	echo "export ALLOCPATH=$alloc_dir" >> $env_var_path
else
	# 替换新的工具环境变量
	sed -i "" "s#$old_alloc_dir#$alloc_dir#g" $env_var_path
fi

if [ -z $old_path ];then
    # 未设置环境变量，添加环境变量
    echo 'export PATH=$PATH:$ALLOCPATH' >> $env_var_path
fi

# 环境变量生效
source $env_var_path

echo -e "\033[;32mInstall platon allocation tool succeed. \033[0m"
